﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class class_ISYC_201_04
    {
        public static void ISYC_201_04(Dictionary<string, Object> data) //平行四边形刀片和长方形刀片
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //基本参数-------------------------------------------------------------------------------------------------
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            //赋值参数--------------------------------------------------------------------------------------------------
            double L = Convert.ToDouble(data["l"]);
            double EPSR = Convert.ToDouble(data["epsr"]);
            double EPSR1 = Convert.ToDouble(data["epsr1"]);
            double W1 = Convert.ToDouble(data["w1"]);
            double S = Convert.ToDouble(data["s"]);
            double AN = Convert.ToDouble(data["an"]);
            double RE = Convert.ToDouble(data["re"]);
            double scale = L / 10;//放大系数

            //点---------------------------------------------------------------------------------------------
            //第一部分
            //轮廓
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = fun.Polar(v1, EPSR * pi / 180, W1 / Math.Cos(EPSR) / 2.0);
            Vector2 v3 = fun.Polar(v1, EPSR * pi / 180, W1 / Math.Cos(EPSR));
            Vector2 v4 = fun.Polar(v3, 0 * pi / 180, L);
            Vector2 v5 = fun.Polar(v1, 0 * pi / 180, L);
            Vector2 v6 = fun.Inters(v3, v5, v1, v4);
            Vector2 v7 = new Vector2(v5.X + L * 2, v5.Y);
            Vector2 v8 = fun.Polar(v7, (180 - AN) * pi / 180, S / Math.Cos(AN * pi / 180));
            Vector2 v10 = new Vector2(v7.X, v7.Y + W1);
            Vector2 v9 = fun.Inters(v8, fun.Polar(v8, 90 * pi / 180, L), v10, fun.Polar(v10, -(180 - AN) * pi / 180, L));
            Vector2 v11 = new Vector2(v7.X, v6.Y);

            //轮廓（长方形）
            Vector2 v1_1 = new Vector2(v1.X + L * 2, v1.Y);
            Vector2 v2_1 = fun.Polar(v1_1, EPSR1 * pi / 180, W1 / 2.0);
            Vector2 v3_1 = fun.Polar(v1_1, EPSR1 * pi / 180, W1);
            Vector2 v4_1 = fun.Polar(v3_1, 0 * pi / 180, L);
            Vector2 v5_1 = fun.Polar(v1_1, 0 * pi / 180, L);
            Vector2 v6_1 = fun.Inters(v3_1, v5_1, v1_1, v4_1);
            Vector2 v7_1 = new Vector2(v5_1.X + L * 2, v5_1.Y);
            Vector2 v8_1 = fun.Polar(v7_1, (180 - AN) * pi / 180, S / Math.Cos(AN * pi / 180));
            Vector2 v10_1 = new Vector2(v7_1.X, v7_1.Y + W1);
            Vector2 v9_1 = fun.Inters(v8_1, fun.Polar(v8_1, 90 * pi / 180, L), v10_1, fun.Polar(v10_1, -(180 - AN) * pi / 180, L));
            Vector2 v11_1 = new Vector2(v7_1.X, v6_1.Y);

            //作图部分-----------------------------------------------------------------------
            //第一部分
            //轮廓
            LwPolyline polyline1 = new LwPolyline();
            polyline1.Vertexes.Add(new LwPolylineVertex(v2));
            polyline1.Vertexes.Add(new LwPolylineVertex(v3));
            polyline1.Vertexes.Add(new LwPolylineVertex(v4));
            polyline1.Vertexes.Add(new LwPolylineVertex(v5));
            polyline1.Vertexes.Add(new LwPolylineVertex(v1));
            polyline1.Vertexes.Add(new LwPolylineVertex(v2));
            fun.PolyFill(dxf, polyline1, 0.1, contour);

            //倒角
            var arc1 = Pus.Fillet4LwPline(polyline1, 1, RE);
            var arc2 = Pus.Fillet4LwPline(arc1, 3, RE);
            var arc3 = Pus.Fillet4LwPline(arc2, 5, RE);
            var arc4 = Pus.Fillet4LwPline(arc3, 7, RE);
            fun.PolyFill(dxf, arc4, 0.3, contour);

            //第二部分
            //轮廓
            LwPolyline polyline2 = new LwPolyline();
            polyline2.Vertexes.Add(new LwPolylineVertex(v2_1));
            polyline2.Vertexes.Add(new LwPolylineVertex(v3_1));
            polyline2.Vertexes.Add(new LwPolylineVertex(v4_1));
            polyline2.Vertexes.Add(new LwPolylineVertex(v5_1));
            polyline2.Vertexes.Add(new LwPolylineVertex(v1_1));
            polyline2.Vertexes.Add(new LwPolylineVertex(v2_1));
            fun.PolyFill(dxf, polyline2, 0.1, contour);

            //倒角
            var arc1_1 = Pus.Fillet4LwPline(polyline2, 1, RE);
            var arc2_1 = Pus.Fillet4LwPline(arc1_1, 3, RE);
            var arc3_1 = Pus.Fillet4LwPline(arc2_1, 5, RE);
            var arc4_1 = Pus.Fillet4LwPline(arc3_1, 7, RE);
            fun.PolyFill(dxf, arc4_1, 0.3, contour);

            //标注---------------------------------------------------------------------------
            double height = 1 * scale;
            double arrowsize = 0.5 * scale;
            double exline = 0.5 * scale;

            //L
            fun.dimrot1(dxf, v1, v5, 180, L * 0.4, 0.1, height, arrowsize, exline, "L", "", "");

            //W1
            fun.dimrot1(dxf, v1, v3, 90, S * 1.5, 0.1, height, arrowsize, exline, "W1", "", "");

            //INSL
            fun.dimrot1(dxf, v3, v4, -(90 - EPSR), W1 / 2.0, 0.1, height, arrowsize, exline, "INSL", "", "");

            //角度标注
            //EPSR
            fun.jdbz2(dxf, new Line(v4, fun.Polar(v4, 0 * pi / 180, 0.1)), new Line(v4, fun.Polar(v4, EPSR * pi / 180, 0.1)), S, height, arrowsize, exline, "EPSR", "", "");

            //RE
            Vector2 re = fun.Polar(v6, fun.CalAngle(v6, v4), fun.CalDistance(v6, v4) - RE / Math.Sin(EPSR / 2.0 * pi / 180) + RE);
            fun.jt(dxf, re, -30, S, 1, "RE");

            //长方形刀片
            //L
            fun.dimrot1(dxf, v1_1, v5_1, 180, L * 0.4, 0.1, height, arrowsize, exline, "L", "", "");

            //W1
            fun.dimrot1(dxf, v1_1, v3_1, 90, S * 1.5, 0.1, height, arrowsize, exline, "W1", "", "");

            //角度标注
            //EPSR
            fun.jdbz2(dxf, new Line(v4_1, fun.Polar(v4_1, 0 * pi / 180, 0.1)), new Line(v4_1, fun.Polar(v4_1, EPSR1 * pi / 180, 0.1)), S, height, arrowsize, exline, "EPSR", "", "");

            //RE
            Vector2 re1 = fun.Polar(v6_1, fun.CalAngle(v6_1, v4_1), fun.CalDistance(v6_1, v4_1) - RE / Math.Sin(EPSR1 / 2.0 * pi / 180) + RE);
            fun.jt(dxf, re1, -30, S, 1, "RE");

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
